package com.tjise.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.eduservice.entity.EduSubject;
import com.tjise.eduservice.entity.excel.ExcelSubjectData;
import com.tjise.eduservice.service.EduSubjectService;
import com.tjise.servicebase.exceptionhandler.TjiseException;

/**
 * @program: tjise_parent
 * @description: EasyExcel的监听器
 * @author: 郑朝文
 * @create: 2021-06-23 10:43
 **/
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {
    // 不能实现数据库的操作
    // 通过将spring中的对象传入（有参构造的传入）
    public EduSubjectService eduSubjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    /**
     * 读取Excel的内容
     *
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        if (excelSubjectData == null) {
            throw new TjiseException(20001, "数据文件为空");
        }
        // 一行一行读取，每次读取都有两个值 ，第一个值为一级分类， 第二个值为二级分类
        // 判断一级分类是否重复
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService, excelSubjectData.getOneSubjectName());
        if (existOneSubject == null) { // 没有相同的一级分类
            existOneSubject = new EduSubject();
            // 设置它parent_id
            existOneSubject.setParentId("0");
            // 设置它的一级分类的名字
            existOneSubject.setTitle(excelSubjectData.getOneSubjectName());
            eduSubjectService.save(existOneSubject);
        }
        // 添加二级分类
        // 获取一级分类的id值
        String pid = existOneSubject.getId();
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, excelSubjectData.getTwoSubjectName(), pid);
        // 判断二级分类是否重复
        if (existTwoSubject == null ){
            existTwoSubject = new EduSubject();
            // 设置它parent_id
            existTwoSubject.setParentId(pid);
            // 设置它的二级分类的名字
            existTwoSubject.setTitle(excelSubjectData.getTwoSubjectName());
            eduSubjectService.save(existTwoSubject);

        }




    }

    // 判断一级分类不能重复添加
    private EduSubject existOneSubject(EduSubjectService eduSubjectService, String oneSubjectName) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        // 查询数据库中是否有一级分类 通过title和pid
        wrapper.eq("title", oneSubjectName);
        wrapper.eq("parent_id", "0");
        EduSubject eduSubject = eduSubjectService.getOne(wrapper);
        return eduSubject;

    }

    // 判断二级分类不能重复添加
    private EduSubject existTwoSubject(EduSubjectService eduSubjectService, String oneSubjectName, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        // 查询数据库中是否有二级分类 通过title和pid
        wrapper.eq("title", oneSubjectName);
        wrapper.eq("parent_id", pid);
        EduSubject twoEduSubject = eduSubjectService.getOne(wrapper);
        return twoEduSubject;


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}


